import pymysql

try:
    connect = pymysql.connect(
        host = '119.45.203.8', # 主机名
        user = 'root',      # 数据库用户名
        password = 'zsy123456',  # 数据库密码
        db = 'pythondemo',      # 数据库名
        charset = 'utf8',   # 字符集编码
        cursorclass = pymysql.cursors.DictCursor, # 游标类型,
        port = 3301  # 端口
    )
except Exception as e:
    print(e)


try:
    data = [(0,'红米8','749','红米8手机',1,'2020-02-23'),
            (0,'荣耀20s','1599','荣耀手机',1,'2020-02-23'),
            (0,'荣耀20','999','荣耀手机',1,'2020-02-23')]
    # 获取游标对象
    with connect.cursor() as cursor:
        # SQL语句

        # 1. 批量执行
        # sql = "insert into goods values(%s,%s,%s,%s,%s,%s)"
        # cursor.executemany(sql,data);

        # 更新
        # sql = 'update goods set name = "荣耀20i" where id = 23'

        # 删除
        sql = 'delete from goods where id = 23'

        # 2. 单条执行SQL语句
        cursor.execute(sql)

        # 提交操作
        connect.commit()


except Exception as e:
    print(e)
finally:
    # 关闭连接
    connect.close()




